package personal.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import resources.dao.DepartmentDAO;
import resources.dao.EmployeeDAO;
import resources.pojo.pe_deptmt;
import resources.pojo.pe_eplye;
import resources.pojo.pe_eplye_detl;
import resources.util.Tool;
import resources.util.HVUtil;

@ManagedBean(name="employeeManagement")
@SessionScoped
public class EmployeeManagement implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	String message;
	public boolean flagInsert = true;
	private String value;
	private ArrayList<SelectItem> listDepartment;
	private ArrayList<pe_eplye> listStaff;
	private ArrayList<pe_eplye> listCandidate;
	private pe_eplye pe_add;
	private pe_eplye_detl pe_add_detail;
	private pe_eplye pe_st_up = null; // update staff
	private pe_eplye pe_can_up = null; // update candidate
	private pe_eplye_detl pe_stdtl_up = null; // update detail staff
	
	public void onloadAddNew(){
		if(!Tool.isPostBack()){
			initObject();
		}
	}
	
	public void onloadUpdateStaff(){
		if(pe_st_up == null){
			Tool.redirect("staff-management.html");
		}else{
			HVUtil.open();
			listDepartment = new ArrayList<SelectItem>();
			listDepartment.addAll(DepartmentDAO.getSelectDepartment());
			pe_stdtl_up = EmployeeDAO.getDetailStaff(pe_st_up.getId());
			HVUtil.close();
			if(pe_stdtl_up == null){
				Tool.redirect("staff-management.html");
			}
		}
	}
	
	public void onloadUpdateCandidate(){
		if(pe_can_up == null){
			Tool.redirect("candidate-management.html");
		}
	}
	
	public void onloadStaffManagement(){
		if(!Tool.isPostBack()){
			Tool.message(message);
			message=null;
			listStaff = new ArrayList<pe_eplye>();
			HVUtil.open();
			listStaff.addAll(EmployeeDAO.getAllStaff());
			HVUtil.close();
		}
	}
	
	public void onloadCandidateManagement(){
		if(!Tool.isPostBack()){
			Tool.message(message);
			message=null;
			listCandidate = new ArrayList<pe_eplye>();
			HVUtil.open();
			listCandidate.addAll(EmployeeDAO.getAllCandidate());
			HVUtil.close();
		}
	}
	
	public void initObject(){
		HVUtil.open();
		listDepartment = new ArrayList<SelectItem>();
		listDepartment.addAll(DepartmentDAO.getSelectDepartment());
		HVUtil.close();
		pe_add = new pe_eplye();
		pe_add_detail = new pe_eplye_detl();
		pe_add.setPe_deptmt(new pe_deptmt());
		flagInsert = true;
		
	}
	
	public void clickToEditStaff(ActionEvent evt){
		try{
			pe_eplye temp = (pe_eplye)evt.getComponent().getAttributes().get("object");
			if(temp == null){
				return;
			}else{
				pe_st_up = temp;
				Tool.redirect("update-staff.html");
			}
		}catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
	
	public void updateStaff(){
		try{
			boolean flag = EmployeeDAO.updateStaff(pe_st_up, pe_stdtl_up);
			if(flag){
				message = Tool.getKey("update-successfully");
				Tool.redirect("staff-management.html");
			}else{
				message = Tool.getKey("update-unsuccessfully");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateCandidate(){
		try{
			boolean flag = EmployeeDAO.updateCandidate(pe_can_up);
			if(flag){
				message = Tool.getKey("update-successfully");
				Tool.redirect("candidate-management.html");
			}else{
				message = Tool.getKey("update-unsuccessfully");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void clickToEditCandidate(ActionEvent evt){
		try{
			pe_eplye temp = (pe_eplye)evt.getComponent().getAttributes().get("object");
			if(temp == null){
				return;
			}else{
				pe_can_up = temp;
				Tool.redirect("update-candidate.html");
			}
		}catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
	
	//add staff chinh thuc
	public void addStaff(){
		try{
			if(!flagInsert){
				return;
			}
			else{
				flagInsert = false;
				if(pe_add_detail.getIyde() == null){pe_add_detail.setIyde(new Date());}
				if(pe_add_detail.getStde() == null){pe_add_detail.setStde(new Date());}
				HVUtil.open();
				boolean flag = EmployeeDAO.addStaff(pe_add, pe_add_detail);
				HVUtil.close();
				if(flag){
					message = Tool.getKey("insert-successfully");
					Tool.redirect("staff-management.html");
				}else{
					message = Tool.getKey("insert-unsuccessfully");
					Tool.message(message);
				}
			}
		}catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
	
	//add ung vien
	public void addCandidate(){
		try{
			if(!flagInsert){
				return;
			}
			else{
				flagInsert = false;
				if(pe_add_detail.getIyde() == null){pe_add_detail.setIyde(new Date());}
				if(pe_add_detail.getStde() == null){pe_add_detail.setStde(new Date());}
				pe_add.setTeid(3);// ung vien
				pe_add.getPe_deptmt().setId(0);
				HVUtil.open();
				boolean flag = EmployeeDAO.addCandidate(pe_add);
				HVUtil.close();
				if(flag){
					message = Tool.getKey("insert-successfully") ;
					Tool.redirect("candidate-management.html");
				}else{
					message = Tool.getKey("insert-unsuccessfully");
					Tool.message(message);
				}
			}
		}catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	public void setPe_add(pe_eplye pe_add) {
		this.pe_add = pe_add;
	}

	public pe_eplye getPe_add() {
		return pe_add;
	}

	public void setListDepartment(ArrayList<SelectItem> listDepartment) {
		this.listDepartment = listDepartment;
	}

	public ArrayList<SelectItem> getListDepartment() {
		return listDepartment;
	}

	public void setPe_add_detail(pe_eplye_detl pe_add_detail) {
		this.pe_add_detail = pe_add_detail;
	}

	public pe_eplye_detl getPe_add_detail() {
		return pe_add_detail;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getValue() {
		return value;
	}

	public void setListStaff(ArrayList<pe_eplye> listStaff) {
		this.listStaff = listStaff;
	}

	public ArrayList<pe_eplye> getListStaff() {
		return listStaff;
	}

	public void setListCandidate(ArrayList<pe_eplye> listCandidate) {
		this.listCandidate = listCandidate;
	}

	public ArrayList<pe_eplye> getListCandidate() {
		return listCandidate;
	}

	public void setPe_can_up(pe_eplye pe_can_up) {
		this.pe_can_up = pe_can_up;
	}

	public pe_eplye getPe_can_up() {
		return pe_can_up;
	}

	public void setPe_st_up(pe_eplye pe_st_up) {
		this.pe_st_up = pe_st_up;
	}

	public pe_eplye getPe_st_up() {
		return pe_st_up;
	}

	public void setPe_stdtl_up(pe_eplye_detl pe_stdtl_up) {
		this.pe_stdtl_up = pe_stdtl_up;
	}

	public pe_eplye_detl getPe_stdtl_up() {
		return pe_stdtl_up;
	}
}
